package cn.kgc.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import cn.kgc.entity.WmsWareInfo;
import cn.kgc.dto.add.WmsWareInfoAddDTO;
import cn.kgc.dto.update.WmsWareInfoUpdateDTO;
import cn.kgc.service.WmsWareInfoService;
import io.swagger.annotations.ApiImplicitParams;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import cn.kgc.commons.vo.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 仓库信息(WmsWareInfo)表控制层
 *
 * @author 石浩杰
 * @since 2022-12-07 10:27:12
 */
@RestController
@RequestMapping("/wms/WareInfo")
@Api
@Validated
public class WmsWareInfoController {
    /**
     * 服务对象
     */
    @Resource
    private WmsWareInfoService wmsWareInfoService;

    /**
     * 分页查询所有数据
     *
     * @param current 分页对象
     * @return 所有数据
     */
    @GetMapping("/list")
    @ApiOperation(value = "分页查询所有数据", produces = "application/json;charset=utf-8",
            httpMethod = "GET", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "name", value = "仓库名", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "address", value = "仓库地址", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "areaCode", value = "区域编码", paramType = "query", dataType = "String")
    })
    @CrossOrigin
    public IPage<WmsWareInfo> selectAll(@RequestParam(defaultValue = "1") int current,
                                        @RequestParam(defaultValue = "5") int pageSize,
                                        @RequestParam(defaultValue = "") String name,
                                        @RequestParam(defaultValue = "") String address,
                                        @RequestParam(defaultValue = "") String areaCode) {
        LambdaQueryWrapper<WmsWareInfo> lambdaQueryWrapper = new QueryWrapper<WmsWareInfo>().lambda();
        lambdaQueryWrapper.like(StringUtils.isNotBlank(name), WmsWareInfo::getName, name);
        lambdaQueryWrapper.like(StringUtils.isNotBlank(address), WmsWareInfo::getAddress, address);
        lambdaQueryWrapper.like(StringUtils.isNotBlank(areaCode), WmsWareInfo::getAreacode, areaCode);
        IPage<WmsWareInfo> page = new Page(current, pageSize);

        return wmsWareInfoService.page(page,lambdaQueryWrapper);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @CrossOrigin
    @ApiOperation(value = "通过主键查询单条数据", produces = "application/json;charset=utf-8",
            httpMethod = "GET", response = Result.class)
    @ApiImplicitParam(name = "id", value = "主键", paramType = "path", dataType = "int")
    public WmsWareInfo selectOne(@PathVariable Serializable id) {
        return this.wmsWareInfoService.getById(id);
    }

    /**
     * 新增数据
     *
     * @param wmsWareInfoDTO 实体对象
     * @return 新增结果
     */
    @PostMapping("/insert")
    @ApiOperation(value = "新增", produces = "application/json;charset=utf-8",
            httpMethod = "POST", response = Result.class)
    @CrossOrigin
    public boolean insert(@RequestBody WmsWareInfoAddDTO wmsWareInfoDTO) {
        WmsWareInfo wmsWareInfo = new WmsWareInfo();
        BeanUtils.copyProperties(wmsWareInfoDTO, wmsWareInfo);
        return this.wmsWareInfoService.save(wmsWareInfo);
    }

    /**
     * 修改数据
     *
     * @param wmsWareInfoDTO 实体对象
     * @return 修改结果
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改信息", produces = "application/json;charset=utf-8",
            httpMethod = "POST", response = Result.class)
    @CrossOrigin
    public boolean update(@RequestBody WmsWareInfoUpdateDTO wmsWareInfoDTO) {
        WmsWareInfo wmsWareInfo = this.wmsWareInfoService.getById(wmsWareInfoDTO.getId());
        BeanUtils.copyProperties(wmsWareInfoDTO, wmsWareInfo);
        return this.wmsWareInfoService.updateById(wmsWareInfo);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除结果
     */
    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "根据主键删除信息", produces = "application/json;charset=utf-8",
            httpMethod = "DELETE", response = Result.class)
    @ApiImplicitParam(name = "id", value = "主键", paramType = "path", dataType = "int")
    @CrossOrigin
    public boolean delete(@PathVariable Long id) {
        return this.wmsWareInfoService.removeById(id);
    }
}
